import QtQuick
import QtQuick.Controls
import Toys 1.0
import QtQuick.Dialogs

// StandardPaths
import QtCore

Item {
    id: root_item

    MenuBar {
        id: menu_bar
        height: 40
        anchors.left: parent.left
        anchors.right: parent.right
        Menu {
            title: qsTr("&File")
            Action {
                text: qsTr("&New...")
                enabled: false
                onTriggered: {
                    console.log("new")
                }
            }
            Action {
                text: qsTr("&Open...")
                onTriggered: open_file_dialog.open()
            }
            Action {
                enabled: false
                text: qsTr("&Save")
            }
            Action {
                enabled: false
                text: qsTr("Save &As...")
            }
            MenuSeparator {}
            Action {
                text: qsTr("&Export")
                onTriggered: handle_export()
            }
            MenuSeparator {}
            Action {
                enabled: false
                text: qsTr("&Quit")
            }
        }
        Menu {
            title: qsTr("&Edit")
            Action {
                enabled: false
                text: qsTr("Cu&t")
            }
            Action {
                enabled: false
                text: qsTr("&Copy")
            }
            Action {
                text: qsTr("&Paste")
                onTriggered: handle_paste()
            }
        }
        Menu {
            enabled: false
            title: qsTr("&Help")
            Action {
                text: qsTr("&About")
            }
        }
    }

    Rectangle {
        id: main_content
        anchors.top: menu_bar.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        color: "#fefeee"
        border.color: "gray"
        border.width: focus ? 1 : 0

        IconTool {
            id: icon_tool
        }

        Image {
            id: image_show
            anchors.centerIn: parent
        }

        MouseArea {
            id: mouseArea
            anchors.fill: parent
            onClicked: parent.focus = true
        }

        DropArea {
            id: dragArea
            anchors.fill: parent

            onDropped: function (drop) {
                if (drop.hasUrls) {
                    image_show.source = load_from_file(drop.urls[0])
                }
            }
        }
    }

    Shortcut {
        sequences: ["Ctrl+V"]
        onActivated: {
            console.log("paste")
            handle_paste()
        }
    }

    FileDialog {
        id: open_file_dialog
        currentFolder: StandardPaths.standardLocations(
                           StandardPaths.PicturesLocation)[0]
        onAccepted: image_show.source = load_from_file(selectedFile)
    }

    Notification {
        id: notification
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        width: 300
    }

    // Column {

    //     Button {
    //         text: "push"
    //         onClicked: notification.notify('Success',
    //                                        'This is a success message',
    //                                        'success')
    //     }
    // }
    function load_from_file(file_url) {
        const img_url = icon_tool.load_from_file(file_url)
        if (img_url.toString().trim().length > 0) {
            notification.notify('Success', 'Load image success', 'success')
        } else {
            notification.notify('Faild', 'Load image failed', 'error')
        }

        return img_url
    }

    function handle_paste() {
        const img_url = icon_tool.load_clipboard()
        console.log('img_url:', typeof img_url.toString())
        if (img_url.toString().trim().length > 0) {
            notification.notify('Success', 'Paste image success', 'success')
        } else {
            notification.notify('Faild', 'Paste image faild', 'error')
        }
        image_show.source = img_url
    }

    function handle_export() {
        if (icon_tool.save_icon()) {
            notification.notify('Success', 'Save icon success', 'success')
        } else {
            notification.notify('Faild', 'Save icon faild', 'error')
        }
    }
}
